<template>
  <div class="article-container">
    <!--面包导航区域  -->
    <breadcrumb></breadcrumb>

    <!-- 塞选卡片1 -->

    <!--  -->
    <el-card style="margin-top: 15px">
      <!-- 标题 -->
      <div slot="header" class="clearfix">
        <el-row :gutter="20" type="flex" justify="flex-end">
          <el-col :span="2">
            <el-button type="primary" @click="bannerAdd">添加</el-button>
          </el-col>
        </el-row>
      </div>

      <!-- 轮播图表格 -->
      <el-table
        :data="banners"
        stripe
        style="width: 100%"
        :default-sort = "{prop:'pubdate',order: 'descending'}"
      >
        <el-table-column prop="img" label="封面" width="180">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.img"
              style="width: 100px; height: 100px"
              :src="scope.row.img"
              fit="fill"
              :preview-src-list="[scope.row.img]"
            ></el-image>
            <img v-else style="width: 100px; height: 100px" src="../../assets/images/noImg.jpg" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="pubdate" label="发布时间" > </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改信息" placement="top">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="bannerEdit(scope.row.id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top" >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteDialog(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.per_page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total_count"
      >
      </el-pagination>
    </el-card>

    <!-- 删除轮播图对话框 -->
    <el-dialog title="删除用户" :visible.sync="deleteDialogVisble" width="50%">
      <span>您确定删除该轮播图吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisble = false">取消</el-button>
        <el-button type="primary" @click="deleteBanner">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from "@/components/Breadcrumb.vue";
import { getBanner,postDeleteBanner} from "@/api/banner";
export default {
  components: {
    Breadcrumb,
  },
  data() {
    return {
      //轮播图表格数据
      banners: [
        // pubdate
      ],
      //轮播图请求参数
      queryInfo: {
        page: 1,
        per_page: 5,
      },
      //轮播图总数
      total_count: null,
      //轮播图删除参数
      deleteParams:{
        id:null
      },
      //添加轮播图
      addDialogVisble:false,
      //删除轮播图对话框显示隐藏
      deleteDialogVisble:false,
    };
  },
  computed: {},
  methods: {
    //获取轮播图数据
    async loadBanners() {
      const { data: res } = await getBanner(this.queryInfo);
      console.log(res);
      this.banners = res.datas.banners;
      console.log(this.banners)
      this.total_count = res.datas.total;
    },
    //监听pape改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      this.loadArticles();
    },
    //监听per_page改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.per_page = newSize;
      this.loadArticles();
    },
    //去轮播图添加区域
    bannerAdd(){
      this.$router.push('/banner/add')
    },
    //去轮播图修改编辑区域
    bannerEdit(id){
      this.$router.push('/banner/edit/'+ id)
    },
    //删除轮播图对话框显示
    deleteDialog(row){
      console.log(row)
      this.deleteDialogVisble = true
      this.deleteParams = {
        id:row.id
      }
    },
    //删除轮播图
    async deleteBanner(){
      const {data:res} = await postDeleteBanner(this.deleteParams)
      if(res.meta.status !== 200) return this.$message.error('删除轮播图信息失败')
      this.$message.success('删除轮播图信息成功')
      this.deleteDialogVisble = false
      this.loadBanners()
    }
  },
  created() {
    //调用轮播图数据
    this.loadBanners();
  },
  mounted() {},
};
</script>

<style lang='less' scoped>
.article-cover {
  width: 50%;
  background-size: cover;
}
</style>